<?php


namespace App\Http\Controllers\Traits;


use App\DTO\BaseDTO;
use App\Exceptions\ApiException;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

trait ResponseBag
{
    /**
     * @param null $data
     * @param string $message
     * @param int $interNalCode
     * @param int $httpCode
     * @param bool $json
     * @return ResponseFactory|JsonResponse|Response
     */
    public function basic($data = null, $message = "SUCCESS", $interNalCode = 20000, $httpCode = 200, $json = true)
    {
        $response = [
            'message' => $message,
            'code' => $interNalCode,
            'data' => $data
        ];

        return $json ? response()->json($response, $httpCode) : response($response, $httpCode);
    }

    /**
     * @param null $data
     * @param string $message
     * @return ResponseFactory|JsonResponse|Response
     */
    public function success($data = null, $message = "SUCCESS")
    {
        return $this->basic($data, $message, 20000, 200);
    }


    /**
     * @param string $message
     * @return ResponseFactory|JsonResponse|Response
     */
    public function failed($message = "Request Failed")
    {
        return $this->basic(null, $message, 40000, 400);
    }

    /**
     * @param string $message
     * @return ResponseFactory|JsonResponse|Response
     */
    public function notfound($message = "Resource Not Found")
    {
        return $this->basic(null, $$message, 40401);
    }


    /**
     * @param ApiException $exception
     * @return ResponseFactory|JsonResponse|Response
     */
    public function renderApiException(ApiException $exception)
    {
        return $this->basic(null, $exception->getMessage(), $exception->getErrorCode(), $exception->getCode());
    }
}